//
//  MulMenuView.m
//  MJMulMenu
//
//  Created by HuiJiaHuDong on 2017/9/7.
//  Copyright © 2017年 HuiJia. All rights reserved.
//

#import "MulMenuView.h"
#import "LeftTableViewCell.h"
#import "RightCollectionView.h"



#pragma mark-- 宏定义

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

#define kLeftWidth 100
#define kScreenWidth [UIScreen mainScreen].bounds.size.width

//一行col的个数
static NSInteger const cols = 3;

//大col左右的间距
static NSInteger const leftMargin = 10.0;

//col中cell的行列间距
static CGFloat const margin = 0.0;

//大col的宽度
#define kleftColWidth (kScreenWidth - kLeftWidth - leftMargin * 2)

//单个col cell的宽度
#define cellWH ((kleftColWidth - (cols - 1) * margin) / cols)

@interface MulMenuView()<UITableViewDataSource , UITableViewDelegate , RightCollectionViewDelegate>

//全部数据
@property(strong,nonatomic,readonly) NSArray * allData;

@property(nonatomic , weak) UITableView * leftTablew;

@property(nonatomic , weak) RightCollectionView * rightColView;

//左边选中的索引
@property(assign,nonatomic,readonly) NSInteger selectIndex;

/**
 *  为了 不修改原来的，因此增加了一个属性，选中指定 行数
 */
@property(assign,nonatomic) NSInteger needToScorllerIndex;

/**
 *  左边背景颜色
 */
@property(strong,nonatomic) UIColor * leftBgColor;
/**
 *  左边点中文字颜色
 */
@property(strong,nonatomic) UIColor * leftSelectColor;
/**
 *  左边点中背景颜色
 */
@property(strong,nonatomic) UIColor * leftSelectBgColor;

/**
 *  左边未点中文字颜色
 */

@property(strong,nonatomic) UIColor * leftUnSelectColor;
/**
 *  左边未点中背景颜色
 */
@property(strong,nonatomic) UIColor * leftUnSelectBgColor;
/**
 *  tablew 的分割线
 */
@property(strong,nonatomic) UIColor * leftSeparatorColor;

@end

@implementation MulMenuView


#pragma mark-- 初始化

/**
 视图由两部分组成:左侧tableView,右侧一个UICollectionView
 
 @param frame 位置
 @param data 数据
 @param startIndex 左侧列表开始索引
 @return 返回值
 */
-(instancetype)initWithFrame:(CGRect)frame WithData:(NSArray<MJMenuItem *> *)data andStartIndex:(NSInteger)startIndex
{
	if (self = [super initWithFrame:frame]) {
		
		if (data.count==0) {
			return nil;
		}
		
		//设置默认值
		//左边点中文字颜色
		self.leftSelectColor=  [UIColor redColor];
		self.leftSelectBgColor = UIColorFromRGB(0xF3F4F6);
		self.leftUnSelectBgColor = [UIColor whiteColor];
		self.leftBgColor = UIColorFromRGB(0xF3F4F6);
		self.leftSeparatorColor = UIColorFromRGB(0xE5E5E5);
		self.leftUnSelectColor = [UIColor blackColor];
		
        //记录所有的数据
		_allData = data;
		_selectIndex = 0;
		
		[self setupUI];
		
		//设置左侧列表初始位置
		self.needToScorllerIndex = startIndex;
		
	}
	return self;
}


#pragma mark-- 设置UI
- (void)setupUI
{
	self.backgroundColor = self.leftSelectBgColor;
	
	[self setupTableView];
	
	[self setupCollectionView];
}

#pragma mark-- tableView和CollectionVIew初始化

- (void)setupTableView
{
    //创建tableVIew
	UITableView *leftTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kLeftWidth, self.frame.size.height)];
	
	[self addSubview:leftTable];
	
	self.leftTablew = leftTable;
	
	[self.leftTablew registerNib:[UINib nibWithNibName:NSStringFromClass([LeftTableViewCell class]) bundle:nil] forCellReuseIdentifier:cellID];
	
	self.leftTablew.dataSource=self;
	
	self.leftTablew.delegate=self;
	
	//这样子能够去掉tableVIew下面的分割线
	self.leftTablew.tableFooterView=[[UIView alloc] init];
	
	//补全分割线
	if ([self.leftTablew respondsToSelector:@selector(setLayoutMargins:)]) {
		self.leftTablew.layoutMargins=UIEdgeInsetsZero;
	}
	if ([self.leftTablew respondsToSelector:@selector(setSeparatorInset:)]) {
		self.leftTablew.separatorInset=UIEdgeInsetsZero;
	}
	//UIColorFromRGB(0xF3F4F6)
	self.leftTablew.separatorColor = self.leftSeparatorColor;

	
	
	
}

- (void)setupCollectionView
{
	UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
	
	layout.minimumInteritemSpacing = margin;//左右间隔
	
	layout.minimumLineSpacing = margin;
	
	layout.itemSize = CGSizeMake(cellWH, cellWH);
	
	RightCollectionView *colView = [[RightCollectionView alloc] initWithFrame:CGRectMake(kLeftWidth + leftMargin, 64.0 , kleftColWidth , self.frame.size.height - 64.0) collectionViewLayout:layout];
	
	colView.clickDelegate = self;
	
	colView.showsVerticalScrollIndicator = NO;
	
	colView.backgroundColor = self.leftSelectBgColor;
	
	[self addSubview:colView];
	
	self.rightColView = colView;
	
	
}

#pragma mark-- tableView的数据源和代理

static NSString *const cellID = @"LeftTableViewCellID";

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
	
	
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return self.allData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	LeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    cell.iconLabel.text = [NSString stringWithFormat:@"第%zd个",indexPath.row];
	
	if (indexPath.row == _selectIndex) {
		
		[self setLeftTablewCellSelected:YES withCell:cell];
		
	}else{
		
		[self setLeftTablewCellSelected:NO withCell:cell];
		
	}
	
	if (indexPath.row < self.allData.count) {
		cell.menuItem = self.allData[indexPath.row];
	}
	
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
	
	if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
		cell.layoutMargins=UIEdgeInsetsZero;
	}
	if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
		cell.separatorInset=UIEdgeInsetsZero;
	}

	return cell;
}

#pragma mark-- tableView的点击



//MARK:- 监听默认选择某一行
- (void)setNeedToScorllerIndex:(NSInteger)needToScorllerIndex
{
    _needToScorllerIndex = needToScorllerIndex;
    
    /**
     *  设置某一行cell选中,并且滚动其对应的位置
        这里遇到一个坑,就是在调整数据的时候,一定要给有区分的标识数据,否则像这里一样
        明明数据有变动,但是因为看不出来,所以浪费了很多时间
		注意,这个方法后面的代码一定得走,不然会错乱
     */

	if (!self.allData || needToScorllerIndex > self.allData.count - 1) NSLog(@"没有数据或者默认选择行大于了选中,tableVIew的行数,请重新设置");

    [self.leftTablew selectRowAtIndexPath:[NSIndexPath indexPathForRow:needToScorllerIndex inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
    
    _selectIndex = needToScorllerIndex;
	
	//刷新col数据
	[self reloadRightColViewData];
}



//选中某行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	
	LeftTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    _selectIndex = indexPath.row;
	
	[self setLeftTablewCellSelected:YES withCell:cell];
	
	//刷新col数据
	[self reloadRightColViewData];
}

#pragma mark-- 给右侧的colVIew传递数据,以及刷新数据
- (void)reloadRightColViewData
{
	/*
	 这里无论是通过cell的数据还是获取indexPath的数据都可以,但是就是没法设置col的初始数据
	 */
	//获取当对应的col的数据
	if (!self.allData || _selectIndex > self.allData.count) return;
	
	MJMenuItem * sectionColItem = self.allData[_selectIndex];
	
	self.rightColView.colDataArr = sectionColItem.nextArray;
	
}

//取消选定某行
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LeftTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    [self setLeftTablewCellSelected:NO withCell:cell];
    
    cell.backgroundColor=self.leftUnSelectBgColor;
    
}


//处理选中后的cell背景色和线的颜色(注意修改模型)
- (void)setLeftTablewCellSelected:(BOOL)selected withCell:(LeftTableViewCell *)cell
{
	
	if (selected)
	{
		cell.backgroundColor = self.leftSelectBgColor;
		
		cell.leftLine.backgroundColor = cell.backgroundColor;
		
		cell.iconLabel.textColor = self.leftSelectColor;
	}else
	{
		cell.leftLine.backgroundColor = _leftTablew.separatorColor;
		
		cell.iconLabel.textColor = self.leftUnSelectColor;
		
		cell.backgroundColor = self.leftUnSelectBgColor;
	}
}

#pragma mark-- CollectionView的代理方法

- (void)rightCollectionView:(RightCollectionView *)colView andClickedIndex:(NSIndexPath *)indexPath
{
	NSLog(@"点击了右侧第%zd栏的第%zd组第%zd个商品",_selectIndex,indexPath.section,indexPath.row);
}


















@end
